<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth, DB;
use App\PatientCase;
use App\PictureInfo;
use App\VideoInfo;
use Redirect, Input;
use Log;

class UploadController extends Controller {
	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index() {
		//
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create() {
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store(Request $request) {
		//
		$response = "Request Successfully Operated";
		$jsonData = $request -> input('data');
		$decodeData = json_decode($jsonData, true);
		$patient_id = $decodeData["User_ID"];
		$user_id = $decodeData["Doctor_Info"]["Doctor_ID"];
		$img_arr = $decodeData["Images"];		
		$base_dir = "D:/wamp/www/medicloud/public";
		$case_dir = "/cases/".$decodeData["case_id"];
		if(!file_exists($base_dir.$case_dir)){
			mkdir($base_dir.$case_dir);
		}		
		for ($index = 0; $index < count($img_arr); ++$index) {
			$photo = new PictureInfo;
			$photo -> left_or_right = ($img_arr[$index]["PhotoType"] == 0) ? "OS" : "OD";
			$photo -> case_id = $decodeData["case_id"];
			$path = $img_arr[$index]["PhotoImagePath"];
			$filepath = $base_dir.$case_dir."/".basename($path);
			rename($path,$filepath);
			$photo->pic_url = $case_dir."/".basename($path);
						
			if($photo->save()){
				Log::info("Photo has been saved @ $filepath");				
			} else {
				Log::warning("Error while saving photo with origin @ $path");
				$response = "Error while handling request.";
			}							 							
		}
		Redirect::to("/");
		return $response;
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show(Request $request) {		
		$id = $request->input('id');			
		$pic = DB::table('picture')->where('case_id',$id)->get();		
		$json_url = json_encode($pic);
				
		return $json_url;	
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit(Request $request) {
		//
		$canvas = $request->input('canvasInfo');
		$id = $request->input('id');
		$picture = PictureInfo::find($id);
		Log::info('start saving canvas info');
		$picture->canvas_info = $canvas;		
	
		if($picture->save()){
			Log::info('canvas saved');
			return 'Update success';
		} else{
			return 'Save failed';
		}
			
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id) {
		
	}
	
	/**
	 * Load requested picture
	 */
	public function load(Request $request){
		
		$id = $request->input('id');		
		$canvasInfo = PictureInfo::find($id)->canvas_info;   
		if(!is_null($canvasInfo)){
			Log::info($canvasInfo);
			return $canvasInfo;
		}	else {
			return "Can not find canvas info for id $id";
		}	
	}
	
	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id) {
		//
	}

}
